import pickle
import numpy as np
import scipy.io.wavfile as wf
import python_speech_features as sf
import sklearn.preprocessing as sp
import sklearn.svm as svm
import os

class AudioRecognizer:
    def __init__(self, model_path):
        self.names = ['abnormal_treble', 'normal_bass', 'normal_bass_inside_the_carriage', 'normal_treble', 'normal_treble_inside_the_carriage', 'resonant_bass_inside_the_carriage', 'resonant_treble_inside_the_carriage']
        with open(model_path, 'rb') as file:
            self.model = pickle.load(file)

    def mfcc(self, file):
        sample_rate, signs = wf.read(file)
        mfc = sf.mfcc(signs, sample_rate)
        sample = np.mean(mfc, axis=0)
        return sample.reshape(1, -1)

    def predict(self, file_path):
        features = self.mfcc(file_path)
        prediction = self.model.predict(features)
        return self.names[prediction[0]]

# recognizer = AudioRecognizer(model_path="model.pickle")
#
# file_path = r"trainvoice5\train\normal_treble\normal_back_treble4.wav"
# predicted_label = recognizer.predict(file_path)
# print(f"Predicted Label: {predicted_label}")
